package me.shurufa.widget.glide;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

public class AdaptiveBitmapImageViewTarget extends BitmapImageViewTarget {
    private ImageView imageView;
    public AdaptiveBitmapImageViewTarget(ImageView view) {
        super(view);
        imageView = view;
    }

    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
        super.onResourceReady(bitmap, anim);
    }

    @Override
    protected void setResource(Bitmap resource) {
        try {
            int adaptiveHeight = resource.getHeight() * imageView.getWidth() / resource.getWidth();
            imageView.setLayoutParams(new FrameLayout.LayoutParams(imageView.getWidth(), adaptiveHeight));
        } catch (Exception e) {
        } finally {
            super.setResource(resource);
        }

    }

    @Override
    public void onLoadFailed(Exception e, Drawable errorDrawable) {
        super.onLoadFailed(e, errorDrawable);
    }

    @Override
    public void onLoadStarted(Drawable placeholder) {
        super.onLoadStarted(placeholder);
    }

    @Override
    public void onLoadCleared(Drawable placeholder) {
        super.onLoadCleared(placeholder);
    }

}
